package com.lhj.xxsc.order.interceptor;

import com.xxsc.common.constant.AuthServerConstant;
import com.xxsc.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 罗惠江
 * @version 1.0
 * @email 1511619294@qq.com
 * @date 2021/1/4 15:05
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {
	public static ThreadLocal<MemberRespVo> loginUser = new ThreadLocal<>();

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String uri = request.getRequestURI();

		boolean pay = new AntPathMatcher().match("/payed/**", uri);
		boolean match = new AntPathMatcher().match("/order/order/status/**", uri);
		if (match || pay) {
			return true;
		}

		MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
		if (attribute != null) {
			loginUser.set(attribute);
			return true;
		} else {
			// 没有登录就去登录
			request.getSession().setAttribute("msg", "请先进行登录");
			response.sendRedirect("http://auth.xxsc.com/login.html");
			return false;
		}

	}
}
